<?php 
/**
 * PHP versions 4 and 5
 * MagicPHP
 * Licensed under The Open Group Test Suite License
 * Redistributions of files must retain the above copyright notice.
 * @copyright     Copyright 2011
 * @link          
 * @package       Model
 * @since         MaginPHP 1.0
 * @license     
 */
?>
<?php
	/**
	 * @desc: This class is used to find root folder 
	 * @author aNKIT kHAMBHATA
	 * @version: 1.0
	 */
	class Magic_FindRoot_Model {
                private $file;
                private $number;
                private $sepertor;
                private $logfile ;
                private $configpath;
		/**
		 * @desc: This is constructor use to define vara 
	 	 * @author aNKIT kHAMBHATA
	 	 * @version: 1.0
		 */
		function __construct(){
                            if(OS != ''){
                                if(strtolower(OS) == 'linux'){
                                    $this->sepertor = LINUX_SEPERATOR;
                                    $this->logfile= LOGFILE_LINUX;
                                    $this->configpath=INI_PATH_LINUX;
                                } elseif(strtolower (OS) == 'winnt') {
                                    $this->sepertor =WINDOW_SEPERATOR;
                                    $this->logfile=LOGFILE_WINDOW;
                                    $this->configpath=INI_PATH_WINDOW;
                                }
                                $path=explode($this->sepertor,INCLUDE_PATH);
                                for($i=0;$i<count($path);$i++){
                                    $pos = strrpos($path[$i],ROOT_FOLDER);
                                    if($pos > 0){
                                      $this->number= $i;
                                      break;
                                    }
                                }
                            } else {
                                if(strtolower(PHP_OS) == 'linux'){
                                    $this->sepertor = LINUX_SEPERATOR;
                                    $this->logfile= LOGFILE_LINUX;
                                    $this->configpath=INI_PATH_LINUX;
                                } elseif(PHP_OS == 'winnt') {
                                    $this->sepertor =WINDOW_SEPERATOR;
                                    $this->logfile=LOGFILE_WINDOW;
                                    $this->configpath=INI_PATH_WINDOW;
                                }
                                $path=explode($this->sepertor,INCLUDE_PATH);
                                for($i=0;$i<count($path);$i++){
                                    $pos = strrpos($path[$i],ROOT_FOLDER);
                                    if($pos > 0){
                                      $this->number= $i;
                                      break;
                                    }
                                }
                                
                                $path=explode($this->sepertor,INCLUDE_PATH);
                                $this->file=$path[$this->number];
                                Magic_Log_Model :: writeLog(OS_NOT_DEFINE_ERROR,$username ='random',$this->file.$this->logfile);
                                echo OS_NOT_DEFINE_ERROR_DISPLAY;
                                exit;
                            }
            		}
		/**
		 * @desc: This class is used to find root folder 
	 	 * @author aNKIT kHAMBHATA
	 	 * @version: 1.0
		 */
		public function findRoot() {
                                    $path=explode($this->sepertor,INCLUDE_PATH);
                                    $this->file=$path[$this->number];
                                    return $this->file.LOG.$this->configpath;
                                }
		/**
		 * @desc: This class is used to find root folder 
	 	 * @author aNKIT kHAMBHATA
	 	 * @version: 1.0
		 */	 
	 	public function findLogFile(){
                                    $path=explode($this->sepertor,INCLUDE_PATH);
                                    $this->file=$path[$this->number];
                                    return $this->file.$this->logfile;
                                 }
	 	public function getRelevantPath($file){
                                    $this->releventerror=new Magic_FindRoot_Model();
                                    if(OS != '') {
                                        if(strtolower(OS) == 'linux'){
                                            $folder=explode("/",$file);
                                        }  elseif(strtolower (OS) == 'window') {
                                            $folder=explode("\\",$file);
                                        }

                                        if(in_array(ROOT_FOLDER,$folder)) {
                                                $key =array_search(ROOT_FOLDER, $folder);
                                        } else {
                                                $error=ROOTFOLDER_ERROR;
                                        }
                                        for($i=0;$i<count($folder);$i++){
                                                if($i > $key && basename ($_SERVER["PHP_SELF"]) != $folder[$i] ) {
                                                        $str.=DOT.SEPERATOR;
                                                }
                                        }
                                        if(!empty($error)){
                                                echo  $error;
                                                Magic_Log_Model :: writeLog(ROOTFOLDER_LOG_ERROR,$username ='random',$this->releventerror->findLogFile());
                                                exit;
                                        } elseif($str != '') {
                                                return $str;
                                        } else {
                                                return $str;
                                        }
                                    } else {
                                        echo OS_NOT_DEFINE_ERROR;
                                        Magic_Log_Model :: writeLog(OS_NOT_DEFINE_ERROR,$username ='random',$this->releventerror->findLogFile());
                                    }
                                }
	 } // End Of Class												
 